﻿using Karbel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Template;

namespace DynaCAD
{
    public partial class TemplateCreator : Window
    {
        public Template.ReferanceTemplate TemplateX;
        public Point? MidPointPosition = null;
        public Path Ellipse = null;

        public TemplateCreator()
        {
            InitializeComponent();
            NewTemplateCreator();
            On.Children.Insert(1, XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GridLineCanvas")));
            Arka.Children.Insert(1, XamlEditor.CloneUIElement.cloneElement((Canvas)FindResource("GridLineCanvas")));
        }

        private void NewTemplateCreator()
        {
            TemplateX = new ReferanceTemplate();
            Data.AddTemplateParts(TemplateX);
            FillTemplate();
        }

        //UpDates Canvas & Tabs
        private void FillTemplate()
        {
            FillTabs();
            FillCanvas();
        }

        //UpDates Tabs
        private void FillTabs()
        {
            TABS.Items.Clear();
            foreach (Template.Type T in TemplateX.PartTypeList)
            {
                TabItem TI = new TabItem();
                TabItemMaker(TI, T);
                TABS.Items.Add(TI);
                MenuItem MenuItem1 = new MenuItem();
                MenuItem1.Header = "Yeni Parça Tipi Ekle";

                MenuItem1.Icon = new System.Windows.Controls.Image
                {
                    Source = new BitmapImage(new Uri("Images/Add.png", UriKind.Relative))
                };
                MenuItem1.Height = 22;

                MenuItem MenuItem2 = new MenuItem();
                MenuItem2.Header = T.PartType + " Parça Tipini Sil";

                MenuItem2.Icon = new System.Windows.Controls.Image
                {
                    Source = new BitmapImage(new Uri("Images/Delete.png", UriKind.Relative))
                };
                MenuItem2.Height = 22;

                MenuItem MenuItem3 = new MenuItem();
                MenuItem3.Header = "Yeniden Adlandır";

                TI.ContextMenu = new ContextMenu();
                TI.ContextMenu.Items.Add(MenuItem1);
                TI.ContextMenu.Items.Add(MenuItem2);
                TI.ContextMenu.Items.Add(MenuItem3);
                MenuItem2.Tag = T;
                MenuItem3.Tag = T;
                MenuItem1.Click += (sender, e) => AddPartTypeClick(sender, e);
                MenuItem2.Click += (sender, e) => RemovePartTypeClick(sender, e);
                MenuItem3.Click += (sender, e) => RenamePartTypeClick(sender, e);
            }
        }

        private void AddPartTypeClick(object sender, RoutedEventArgs e)
        {
            TemplateX.PartTypeList.Add(new Template.Type("Yeni Parça", true, true));
            Data.AddNewPart();
            FillTabs();
        }
        private void RemovePartTypeClick(object sender, RoutedEventArgs e)
        {
            MenuItem MI = e.Source as MenuItem;
            Template.Type type = (Template.Type)MI.Tag;
            TemplateX.PartTypeList.Remove(type);
            Data.RemovePart(type.PartType);
            FillTabs();
        }
        private void RenamePartTypeClick(object sender, RoutedEventArgs e)
        {
            MenuItem MI = e.Source as MenuItem;
            Template.Type type = (Template.Type)MI.Tag;
            string first = type.PartType;

            RenamePartType RPT = new RenamePartType(type);

            if (RPT.ShowDialog() == RPT.DialogResult.Value)
            {
                string last = RPT.T.PartType;
                Data.RenamePart(first, last);

                FillTabs();
            }
        }

        private void SubTabItemMaker(TabItem TI, DockPanel D, List<PartPoint> P)
        {
            D.Children.Clear();
            foreach (PartPoint PP in P)
            {
                TextBox TextBoxX = new TextBox();
                TextBoxX.Text = "X:" + Convert.ToString((int)PP.X) + ", Y:" + Convert.ToString((int)PP.Y);
                TextBoxX.IsReadOnly = true;
                TextBoxX.BorderThickness = new Thickness(0);
                DockPanel.SetDock(TextBoxX, Dock.Top);
                D.Children.Add(TextBoxX);
            }
            TI.Content = D;
        }

        //UpDate RelatedTab
        private void TabItemMaker(TabItem TI, Template.Type T)
        {
            TI.Header = T.PartType;
            DockPanel D = new DockPanel();
            TI.Content = D;
            //add Texblock//
            TextBlock TB = new TextBlock();
            TB.Text = T.PartType;
            TB.TextAlignment = TextAlignment.Center;
            DockPanel.SetDock(TB, Dock.Top);
            TB.Background = Brushes.LightBlue;
            TB.VerticalAlignment = VerticalAlignment.Top;
            TB.Height = 20;
            D.Children.Add(TB);
            //
            //add TabControl//
            TabControl TC = new TabControl();
            DockPanel.SetDock(TC, Dock.Top);
            TC.TabStripPlacement = Dock.Top;
            D.Children.Add(TC);


            //OnSol
            TabItem TIOnSol = new TabItem();
            TIOnSol.Header = "ÖnSol";
            TC.Items.Add(TIOnSol);
            DockPanel GONSol = new DockPanel();
            SubTabItemMaker(TIOnSol, GONSol, T.FrontLeftPoints);

            //OnSağ
            TabItem TIOnSag = new TabItem();
            TIOnSag.Header = "ÖnSağ";
            TC.Items.Add(TIOnSag);
            DockPanel GONSag = new DockPanel();
            SubTabItemMaker(TIOnSag, GONSag, T.FrontRightPoints);

            //Arkasol
            TabItem TIASol = new TabItem();
            TIASol.Header = "ArkaSol";
            TC.Items.Add(TIASol);
            DockPanel GASol = new DockPanel();
            SubTabItemMaker(TIASol, GASol, T.BackLeftPoints);

            //ArkaSağ
            TabItem TIASag = new TabItem();
            TIASag.Header = "ArkaSağ";
            TC.Items.Add(TIASag);
            DockPanel GASag = new DockPanel();
            SubTabItemMaker(TIASag, GASag, T.BackRightPoints);


            //add CheckBoxes//
            CheckBox FrontBack = new CheckBox();
            CheckBox LeftRight = new CheckBox();
            FrontBack.IsChecked = true;
            LeftRight.IsChecked = true;

            FrontBack.Checked += (sender, e) => FrontBackChecked(sender, e, TI, T);
            FrontBack.Unchecked += (sender, e) => FrontBackUnChecked(sender, e, TI, T);
            LeftRight.Checked += (sender, e) => LeftRightChecked(sender, e, TI, T);
            LeftRight.Unchecked += (sender, e) => LeftRightUnChecked(sender, e, TI, T);


            FrontBack.IsChecked = T.FrontBack;
            LeftRight.IsChecked = T.LeftRight;

            FrontBack.Height = 20;
            LeftRight.Height = 20;
            FrontBack.VerticalAlignment = VerticalAlignment.Bottom;
            FrontBack.HorizontalAlignment = HorizontalAlignment.Left;
            LeftRight.HorizontalAlignment = HorizontalAlignment.Right;
            LeftRight.VerticalAlignment = VerticalAlignment.Bottom;

            FrontBack.Content = "Ön Arka";
            LeftRight.Content = "Sağ Sol";
            D.Children.Add(FrontBack);
            D.Children.Add(LeftRight);
        }

        //UpDate Canvas
        private void FillCanvas()
        {
            DrawPoints();
        }

        //UpDate Points
        private void DrawPoints()
        {
            TemplatePointsOn.Children.Clear();
            TemplatePointsArka.Children.Clear();
            foreach (Template.Type T in TemplateX.PartTypeList)
            {
                foreach (PartPoint P in T.FrontLeftPoints)
                    TemplatePointsOn.Children.Add(P.Ellipse);
                if (T.LeftRight)
                {
                    foreach (PartPoint P in T.FrontRightPoints)
                        TemplatePointsOn.Children.Add(P.Ellipse);
                }
                if (T.FrontBack)
                {
                    foreach (PartPoint P in T.BackLeftPoints)
                        TemplatePointsArka.Children.Add(P.Ellipse);
                }
                if (T.FrontBack && T.LeftRight)
                {
                    foreach (PartPoint P in T.BackRightPoints)
                        TemplatePointsArka.Children.Add(P.Ellipse);
                }
            }
        }

        //Events
        protected void FrontBackChecked(object sender, RoutedEventArgs e, TabItem TI, Template.Type T)
        {
            T.FrontBack = true;
            TabControl TC = ((DockPanel)(TI.Content)).Children.OfType<TabControl>().First<TabControl>();
            if (T.LeftRight)
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {
                    tabıtem.Visibility = Visibility.Visible;
                    if (i == 0)
                    { tabıtem.Header = "ÖnSol"; }
                    else if (i == 1)
                    { tabıtem.Header = "ÖnSağ"; }
                    else if (i == 2)
                    { tabıtem.Header = "ArkaSol"; }
                    else
                    { tabıtem.Header = "ArkaSağ"; }
                    ++i;
                }
            }
            else
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {

                    if (i == 0)
                    {
                        tabıtem.Header = "Ön";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 1)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else if (i == 2)
                    {
                        tabıtem.Header = "Arka";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else
                    {

                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    ++i;
                }
            }
            DrawPoints();
        }
        protected void FrontBackUnChecked(object sender, RoutedEventArgs e, TabItem TI, Template.Type T)
        {
            T.FrontBack = false;
            TabControl TC = ((DockPanel)(TI.Content)).Children.OfType<TabControl>().First<TabControl>();
            if (T.LeftRight)
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {
                    if (i == 0)
                    {
                        tabıtem.Header = "ÖnSol";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 1)
                    {
                        tabıtem.Header = "ÖnSağ";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 2)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    ++i;
                }
            }
            else
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {

                    if (i == 0)
                    {
                        tabıtem.Header = "Ön";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 1)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else if (i == 2)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    ++i;
                }
            }
            DrawPoints();
        }
        protected void LeftRightChecked(object sender, RoutedEventArgs e, TabItem TI, Template.Type T)
        {
            T.LeftRight = true;
            TabControl TC = ((DockPanel)(TI.Content)).Children.OfType<TabControl>().First<TabControl>();
            if (T.FrontBack)
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {
                    if (i == 0)
                    {
                        tabıtem.Header = "ÖnSol";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 1)
                    {
                        tabıtem.Header = "ÖnSağ";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 2)
                    {
                        tabıtem.Header = "ArkaSol";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        tabıtem.Header = "ArkaSağ";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    ++i;
                }
            }
            else
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {
                    if (i == 0)
                    {
                        tabıtem.Header = "ÖnSol";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 1)
                    {
                        tabıtem.Header = "ÖnSağ";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 2)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    ++i;
                }
            }
            DrawPoints();
        }
        protected void LeftRightUnChecked(object sender, RoutedEventArgs e, TabItem TI, Template.Type T)
        {
            T.LeftRight = false;
            TabControl TC = ((DockPanel)(TI.Content)).Children.OfType<TabControl>().First<TabControl>();
            if (T.FrontBack)
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {
                    tabıtem.Visibility = Visibility.Visible;
                    if (i == 0)
                    {
                        tabıtem.Visibility = Visibility.Visible;
                        tabıtem.Header = "Ön";
                    }
                    else if (i == 1)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else if (i == 2)
                    {
                        tabıtem.Visibility = Visibility.Visible;
                        tabıtem.Header = "Arka";
                    }
                    else
                    { tabıtem.Visibility = Visibility.Collapsed; }
                    ++i;
                }
            }
            else
            {
                int i = 0;
                foreach (TabItem tabıtem in TC.Items)
                {
                    if (i == 0)
                    {
                        tabıtem.Header = "Ön";
                        tabıtem.Visibility = Visibility.Visible;
                    }
                    else if (i == 1)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else if (i == 2)
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        tabıtem.Visibility = Visibility.Collapsed;
                    }
                    ++i;
                }
            }
            DrawPoints();
        }
        private void MainTabSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (TabItem T in TABS.Items.OfType<TabItem>().ToList<TabItem>())
            {
                if (T.IsSelected)
                {
                    Template.Type Type = new Template.Type("null", true, true);
                    foreach (Template.Type type in TemplateX.PartTypeList)
                    {
                        if (type.PartType == (string)T.Header)
                            Type = type;
                    }
                    int i = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>().SelectedIndex;

                    foreach (PartPoint P in Type.FrontLeftPoints)
                        P.Aktive = i == 0;
                    foreach (PartPoint P in Type.FrontRightPoints)
                        P.Aktive = i == 1;
                    foreach (PartPoint P in Type.BackLeftPoints)
                        P.Aktive = i == 2;
                    foreach (PartPoint P in Type.BackRightPoints)
                        P.Aktive = i == 3;
                }
                else
                {
                    Template.Type Type = new Template.Type("null", true, true);
                    foreach (Template.Type type in TemplateX.PartTypeList)
                    {
                        if (type.PartType == (string)T.Header)
                            Type = type;
                    }
                    foreach (PartPoint P in Type.FrontLeftPoints)
                        P.Aktive = false;
                    foreach (PartPoint P in Type.FrontRightPoints)
                        P.Aktive = false;
                    foreach (PartPoint P in Type.BackLeftPoints)
                        P.Aktive = false;
                    foreach (PartPoint P in Type.BackRightPoints)
                        P.Aktive = false;
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            foreach (Window W in Application.Current.Windows)
            {
                try
                {
                    W.Visibility = Visibility.Visible;
                }
                catch { }
            }
        }

        private void RefreshPoints(MouseEventArgs e)
        {
            foreach (TabItem T in TABS.Items.OfType<TabItem>().ToList<TabItem>())
            {
                if (T.IsSelected)
                {
                    Template.Type Type = new Template.Type("null", true, true);
                    foreach (Template.Type type in TemplateX.PartTypeList)
                    {
                        if (type.PartType == (string)T.Header)
                            Type = type;
                    }
                    int i = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>().SelectedIndex;

                    TabControl TC = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>();
                    TabItem TI = TC.Items.OfType<TabItem>().ToList<TabItem>().Find(p => p.IsSelected);

                    if (i == 0)
                    {
                        PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelOn).X, e.GetPosition(TemplateStackPanelOn).Y, Type.FrontLeftPoints.Count);
                        SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.FrontLeftPoints);
                    }
                    else if (i == 1)
                    {
                        PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelOn).X, e.GetPosition(TemplateStackPanelOn).Y, Type.FrontRightPoints.Count);
                        SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.FrontRightPoints);
                    }
                    else if (i == 2)
                    {
                        PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelArka).X, e.GetPosition(TemplateStackPanelArka).Y, Type.BackLeftPoints.Count);
                        SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.BackLeftPoints);
                    }
                    else if (i == 3)
                    {
                        PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelArka).X, e.GetPosition(TemplateStackPanelArka).Y, Type.BackRightPoints.Count);
                        SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.BackRightPoints);
                    }
                }
            }
        }

        //Mouse Events//
        private void AddPointToTemplate(MouseButtonEventArgs e, bool On)
        {
            foreach (TabItem T in TABS.Items.OfType<TabItem>().ToList<TabItem>())
            {
                if (T.IsSelected)
                {
                    Template.Type Type = new Template.Type("null", true, true);
                    foreach (Template.Type type in TemplateX.PartTypeList)
                    {
                        if (type.PartType == (string)T.Header)
                            Type = type;
                    }


                    int i = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>().SelectedIndex;



                    if (i == 0)
                    {
                        if (On)
                        {
                            TabControl TC = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>();
                            TabItem TI = TC.Items.OfType<TabItem>().ToList<TabItem>().Find(p => p.IsSelected);
                            PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelOn).X, e.GetPosition(TemplateStackPanelOn).Y, Type.FrontLeftPoints.Count + 1);

                            if (Type.FrontLeftPoints.Count < 2)
                                Type.FrontLeftPoints.Add(P);
                            else if (Type.FrontLeftPoints.Count == 2)
                            {
                                Type.FrontLeftPoints.Clear();
                                Type.FrontLeftPoints.Add(P);
                            }


                            SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.FrontLeftPoints);
                            DrawPoints();
                        }
                        else
                        {
                            MessageBox.Show("Ön görünüş üzerinden bir nokta seçin !");
                            return;
                        }

                    }
                    else if (i == 1)
                    {
                        if (On)
                        {
                            TabControl TC = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>();
                            TabItem TI = TC.Items.OfType<TabItem>().ToList<TabItem>().Find(p => p.IsSelected);
                            PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelOn).X, e.GetPosition(TemplateStackPanelOn).Y, Type.FrontRightPoints.Count + 1);

                            if (Type.FrontRightPoints.Count < 2)
                                Type.FrontRightPoints.Add(P);
                            else if (Type.FrontRightPoints.Count == 2)
                            {
                                Type.FrontRightPoints.Clear();
                                Type.FrontRightPoints.Add(P);
                            }


                            SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.FrontRightPoints);
                            DrawPoints();
                        }
                        else
                        {
                            MessageBox.Show("Arka görünüş üzerinde bir nokta seçin!");
                            return;
                        }
                    }
                    else if (i == 2)
                    {
                        if (On)
                        {
                            MessageBox.Show("Arka görünüş üzerinde bir nokta seçin!");
                            return;
                        }
                        else
                        {
                            TabControl TC = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>();
                            TabItem TI = TC.Items.OfType<TabItem>().ToList<TabItem>().Find(p => p.IsSelected);
                            PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelArka).X, e.GetPosition(TemplateStackPanelArka).Y, Type.BackLeftPoints.Count + 1);

                            if (Type.BackLeftPoints.Count < 2)
                                Type.BackLeftPoints.Add(P);
                            else if (Type.BackLeftPoints.Count == 2)
                            {
                                Type.BackLeftPoints.Clear();
                                Type.BackLeftPoints.Add(P);
                            }
                            SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.BackLeftPoints);
                            DrawPoints();
                        }
                    }
                    else if (i == 3)
                    {
                        if (On)
                        {
                            MessageBox.Show("Arka görünüş üzerinde bir nokta seçin!");
                            return;
                        }
                        else
                        {
                            TabControl TC = ((DockPanel)(T.Content)).Children.OfType<TabControl>().First<TabControl>();
                            TabItem TI = TC.Items.OfType<TabItem>().ToList<TabItem>().Find(p => p.IsSelected);
                            PartPoint P = new PartPoint(e.GetPosition(TemplateStackPanelArka).X, e.GetPosition(TemplateStackPanelArka).Y, Type.BackRightPoints.Count + 1);

                            if (Type.BackRightPoints.Count < 2)
                                Type.BackRightPoints.Add(P);
                            else if (Type.BackRightPoints.Count == 2)
                            {
                                Type.BackRightPoints.Clear();
                                Type.BackRightPoints.Add(P);
                            }

                            SubTabItemMaker(TI, ((DockPanel)(TI.Content)), Type.BackRightPoints);
                            DrawPoints();
                        }
                    }
                    break;

                }
            }

            return;
        }
        private void StackPanelOnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.Source is Path)
            {
                if (((Path)(e.Source)).Fill != Brushes.Gray && ((Path)(e.Source)).Data is EllipseGeometry)
                {
                    Ellipse = e.Source as Path;

                }
                else
                {
                    AddPointToTemplate(e, true);
                    return;
                }

            }
            else
            {
                AddPointToTemplate(e, true);
                return;
            }

        }
        private void StackPanelArkaMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.Source is Path)
            {
                if (((Path)(e.Source)).Fill != Brushes.Gray && ((Path)(e.Source)).Data is EllipseGeometry)
                {
                    Ellipse = e.Source as Path;

                }
                else
                {
                    AddPointToTemplate(e, false);
                    return;
                }
            }
            else
            {
                AddPointToTemplate(e, false);
                return;
            }
        }
        private void StackPanelOnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Ellipse = null;

        }
        private void StackPanelArkaMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Ellipse = null;

        }
        private void StackPanelOnMouseMove(object sender, MouseEventArgs e)
        {
            RefreshPoints(e);

            System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("/Images/CrossCursor.cur", UriKind.Relative));
            this.Cursor = new System.Windows.Input.Cursor(info.Stream);
            if (Mouse.MiddleButton == MouseButtonState.Pressed)
            {
                if (MidPointPosition != null)
                {
                    Point p = e.GetPosition(On);
                    XO1.TransformationOperations.MoveUIElement(TemplateStackPanelOn, p.X - MidPointPosition.Value.X, p.Y - MidPointPosition.Value.Y);
                    MidPointPosition = p;
                }
                else
                {
                    MidPointPosition = e.GetPosition(On);
                }
            }
            else
            {
                MidPointPosition = e.GetPosition(On);
            }
            if (Ellipse != null && Mouse.LeftButton == MouseButtonState.Pressed)
            {
                ((EllipseGeometry)(Ellipse.Data)).Center = e.GetPosition(TemplateStackPanelOn);
                TemplateX.UpdateTemplate();
            }
        }
        private void StackPanelArkaMouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("/Images/CrossCursor.cur", UriKind.Relative));
            this.Cursor = new System.Windows.Input.Cursor(info.Stream);
            if (Mouse.MiddleButton == MouseButtonState.Pressed)
            {
                if (MidPointPosition != null)
                {
                    Point p = e.GetPosition(Arka);
                    XO1.TransformationOperations.MoveUIElement(TemplateStackPanelArka, p.X - MidPointPosition.Value.X, p.Y - MidPointPosition.Value.Y);
                    MidPointPosition = p;
                }
                else
                {
                    MidPointPosition = e.GetPosition(Arka);
                }
            }
            else
            {
                MidPointPosition = e.GetPosition(Arka);
            }
            if (Ellipse != null && Mouse.LeftButton == MouseButtonState.Pressed)
            {
                ((EllipseGeometry)(Ellipse.Data)).Center = e.GetPosition(TemplateStackPanelArka);
                TemplateX.UpdateTemplate();
            }
        }
        private void StackPanelOnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(TemplateStackPanelOn, e.Delta > 0, e.GetPosition(TemplateStackPanelOn));
        }
        private void StackPanelArkaMouseWheel(object sender, MouseWheelEventArgs e)
        {
            XO1.TransformationOperations.ScaleUIElement(TemplateStackPanelArka, e.Delta > 0, e.GetPosition(TemplateStackPanelArka));
        }
        private void StackPanelOnMouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;
            Ellipse = null;
            MidPointPosition = null;
        }
        private void StackPanelArkaMouseLeave(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;
            Ellipse = null;
            MidPointPosition = null;
        }


        #region Düzelt

        //Button Events//
        private void FrontModelOpenClick(object sender, RoutedEventArgs e)
        {
            OpenOnArka(TemplateCanvasOn);
        }

        private void OpenOnArka(Canvas canvas)
        {
            System.IO.Stream MyStream = null;
            System.Windows.Forms.OpenFileDialog ofld = new System.Windows.Forms.OpenFileDialog();
            ofld.Filter = "svg files (*.svg)|*.svg";
            ofld.RestoreDirectory = true;
            if (ofld.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    if ((MyStream = ofld.OpenFile()) != null)
                    {
                        MyStream.Close();
                        String FileName = ofld.FileName;

                        Viewbox VB = SP1.Convertor.Svg2Viewbox(FileName);
                        if (VB != null)
                        {
                            Canvas C;
                            try
                            {
                                C = (Canvas)VB.Child;
                            }
                            catch
                            {
                                MessageBox.Show("Dosya Açılamadı");
                                return;
                            }
                            VB.Child = null;
                            canvas.Children.Clear();
                            canvas.Children.Add(C);
                        }
                        else
                        {
                            MessageBox.Show("Dosya Açılamadı"); return;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Dosya Açılamadı");
                    return;
                }
            }
        }

        private void BackModelOpenClick(object sender, RoutedEventArgs e)
        {
            OpenOnArka(TemplateCanvasArka);
        }

        #endregion


        private void TemplateSaveClick(object sender, RoutedEventArgs e)
        {
            //Checks for saving
            if (TemplateCanvasOn.Children.Count < 1)
            {
                MessageBox.Show("Şablona ön görünüş ekleyin!");
                return;
            }
            if (TemplateCanvasArka.Children.Count < 1)
            {
                MessageBox.Show("Şablona arka görünüş ekleyin!");
                return;
            }
            if (TemplateName.Text == "")
            {
                MessageBox.Show("Şablon ismini yazın!");
                return;
            }

            //Get Name//
            TemplateX.Name = TemplateName.Text;

            //Get PreparedBy//
            TemplateX.PreparedBy = "KARBEL";  //PreparedBy.Text;

            //Get Front Model
            TemplateX.ModelOn = (Canvas)TemplateCanvasOn.Children[0];

            //Get Back Model
            TemplateX.ModelArka = (Canvas)TemplateCanvasArka.Children[0];

            //Save Template
            //TemplateX.SaveTemplate();

            Data.SaveTemplate(TemplateX);

            MessageBox.Show("Şablon kaydedildi!");
        }
        private void TemplateOpenClick(object sender, RoutedEventArgs e)
        {
            Karbel.Sablonlar templates = new Sablonlar();
            templates.ShowDialog();

            if (templates.Sablon == null)
                return;

            TemplateX = templates.Sablon;

            TemplateCanvasOn.Children.Clear();
            TemplateCanvasOn.Children.Add(TemplateX.ModelOn);

            TemplateCanvasArka.Children.Clear();
            TemplateCanvasArka.Children.Add(TemplateX.ModelArka);

            FillTemplate();
            TemplateName.Text = TemplateX.Name;
        }

        private void FronViewClick(object sender, RoutedEventArgs e)
        {
            TemplateStackPanelOn.RenderTransform = null;
        }
        private void BackViewClick(object sender, RoutedEventArgs e)
        {
            TemplateStackPanelArka.RenderTransform = null;
        }
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F1)
            {
                pvdcd.Project.OpenHelp();
            }
        }
    }
}
